/**
  * related to repairRecord.ui
 * 
 * @Author : 2037630@163.com
 * @Timestamp : 2017-04-19
 */
var Controller = require("core/controller");
var net = require('util/net')
var log = require("util/log")
var constant = require('config/constant')
var storage = require('util/storage')
var navigate = require('util/navigate')
var server = require('config/server')
var router = require('config/router')
var notify = sm('do_Notification')
var filter = require('util/filter')
var common = require('util/common')
var repairBg=ui("repairBg")
Controller({
	config:{
		name : "repairRecord",
		header:{
			title:"我的修复申请"
		}
	},
	mapping : {
		"repairRecord" : "repairData"
	},
	data : {
		repairData : [],
		params:{
			lastShowIndex:0
		}
		
	},
	ui:{
		repairBg: "repairBg",
		repairRecord:"repairRecord"
	},
	methods:{
		goBlank:function(){
//			无数据时载入空白页
			if(this.data.repairData.length==0){
				this.ui.repairBg.bgImage="source://image/app/blank.png"
				this.ui.repairRecord.visible=false
			}
			else{
				this.ui.repairBg.bgImage=""
				this.ui.repairRecord.visible=true
			}
			log.info(this.data.params.bg)
			this.refresh('$')
		},
		loadRepairList:function(){
			return net.send({
				server : server.organization.repairRecord,
				data:{
					sort:"createTime,desc",
					isFlag:1
				},
				success : function(data) {
					log.info("222222")
					log.info(data.content)
					var list = data.content.map(function(x) {
						return {
							reapAppstatu : filter.typeCodeConvert(x.reapAppstatu),
							title : filter.dateConvert(x.createTime),
							reapRemark : x.reapRemark,
							reapOrdernum : x.reapOrdernum,
							reapCust : x.reapCust,
							createTime:x.createTime,
							repairTime : common.dateFormat(x.createTime,'hh:mm'),
							fr_smallBow:""

						}
					})
//					判断是否为首个需要点亮的圆点
					for (var i = 0; i < list.length; i++) {
						list[i].showTitle = !(i!=0&&list[i-1].title===list[i].title)
						if(i===0){
							list[i].fr_smallBow="#0091DCFF"
								log.info("ahahah")
								log.info(list[i].fr_smallBow)
						}else{
							list[i].fr_smallBow="#E4E4E4E4"
						}
					}
					this.data.repairData = list
					log.info("rrrrr")
					log.info(this.data.repairData);
//					this.goBlank()
					this.refresh('repairRecord');

				},
				error : function(data) {
//					this.goBlank()
				},
				scope : this
			})
		},
		checkoutDetail:function(index){
			navigate.redirectTo({
				view:router.user.repairRecDetail,
				data:{
					detail:{
						reapAppstatu:this.data.repairData[index].reapAppstatu,
						reapRemark:this.data.repairData[index].reapRemark,
						reapOrdernum:this.data.repairData[index].reapOrdernum,
						reapCust:this.data.repairData[index].reapCust,
						createTime:this.data.repairData[index].createTime
					}
				}
			})
		}
	},
	event:{
		'repairRecord':{
			'scroll':function(index) {
				log.info(index)
				// 判断是否需要修改圆点颜色
				if(this.data.params.lastShowIndex===index.firstVisiblePosition){
					return
				}else{
					this.data.params.lastShowIndex  = index.firstVisiblePosition
					log.info(this.data.params.lastShowIndex)
				}
				
				// 更新当前节点颜色
				this.data.repairData.each(function(index,item){
					var targetColor = index===this.data.params.lastShowIndex?"#0091DCFF":"#E4E4E4E4"
					if(this.data.repairData[index].fr_smallBow!==targetColor){
						this.data.repairData[index].fr_smallBow = targetColor
					}
					
				},this)
				this.refresh('repairRecord')
			},
			"touch":function(index){
				this.checkoutDetail(index)
			}
		}
	},
	loaded : function() {
		this.loadReady([this.loadRepairList()])
	}
})
